package managedbean;

import java.sql.Timestamp;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import session.stateless.EventSessionBean;
import util.exception.VenueConflictException;

@ManagedBean(name = "eventManagerBean")
@RequestScoped
public class EventManagerBean {

    @EJB
    private EventSessionBean eventSessionBean;
    private String eventName;
    private String startDateTime;
    private String endDateTime;
    private String venue;
    private String systemUser;
    private String eventNameLength;
    private String statusMessage;
    private Long newEventId;

    public EventManagerBean() {
        eventNameLength = "Current event name length is less than 4.";
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getStartDateTime() {
        return startDateTime;
    }

    public void setStartDateTime(String startDateTime) {
        this.startDateTime = startDateTime;
    }

    public String getEndDateTime() {
        return endDateTime;
    }

    public void setEndDateTime(String endDateTime) {
        this.endDateTime = endDateTime;
    }

    public String getVenue() {
        return venue;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }

    public String getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(String systemUser) {
        this.systemUser = systemUser;
    }

    public void setEventNameLength(String eventNameLength) {
        this.eventNameLength = eventNameLength;
    }

    public String getEventNameLength() {
        return eventNameLength;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Long getNewEventId() {
        return newEventId;
    }

    public void setNewEventId(Long newEventId) {
        this.newEventId = newEventId;
    }

    public void saveNewEvent(ActionEvent event) {
        Timestamp tsStartDateTime = Timestamp.valueOf(startDateTime);
        Timestamp tsEndDateTime = Timestamp.valueOf(endDateTime);
        Long venueId = Long.valueOf(venue);
        Long systemUserId = Long.valueOf(systemUser);
        try {
            newEventId = eventSessionBean.addNewEvent(eventName, tsStartDateTime, tsEndDateTime, venueId, systemUserId);
            statusMessage = "New Event Saved Successfully";
        } catch (VenueConflictException vex) {
            statusMessage = "Venue Conflict Exception";
            newEventId = -1L;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doAjaxCountEventNameLength() {
        if (eventName != null) {
            eventNameLength = Integer.toString(eventName.length());
        } else {
            eventNameLength = "0";
        }
        eventNameLength = "Current event name length is " + eventNameLength;
    }
}